<?php

include_once 'BController.php';
 
class main extends BController{
	
	var $pageSize = 5;
	
	var $Wechat = null;
	
	function test(){
		
	}
	
	/**
	* 入口页面 
	* 检测用户是否登陆  是否带有分享者的ID
	* @return
	*/
	function index(){  
		//$this->chkMobile();
		//获取用户ID 
		$hasUidCookie = empty($_COOKIE["UID"])?false:true;
		if(!$hasUidCookie ){
			$this->jump('index.php?c=main&a=wlogin'); // 跳转到微信登陆页面
		}
		
		$uid = $_COOKIE["UID"];
		 
		$signPackage = $this->Wechat->getJsSign('http://pingan.weixinzjit.com/index.php/main/index');		
		$this->JK = $signPackage;
		
		//如果当前用户发起过活动 则会转到view页面
		$gModel = spClass('m_wx_game');
		$gm = $gModel->find(array('uid'=>$uid)); 
		if(!empty($gm)){
			$this->jump('/index.php/main/view'); // 跳转到微信登陆页面
		}
		
		$this->openid = $uid; 
		$uModel = spClass('m_wx_user');
		$my = $uModel->find(array('openid'=>$uid));
		$my['nickname'] = base64_decode($my['nickname']);
		$this->my = $my;
	}
	
	function talk(){ 
		//$this->chkMobile();
		$uid = $_COOKIE['UID']; 
		$random = $this->spArgs('random');
		$gModel = spClass('m_wx_game');
		$gid = $this->spArgs('gid');  
		
		if(empty($uid)){
			if(!empty($random)){
				$ul = 'http://pingan.weixinzjit.com/index.php/main/talk/random/'.$random;
				$this->jump('/index.php?c=main&a=wlogin&turl='.($ul));
			}
			if(!empty($gid)){
				$uk = 'http://pingan.weixinzjit.com/index.php/main/talk/gid/'.$gid;
				$this->jump('/index.php?c=main&a=wlogin&turl='.($ul));
			}
		}
		if(empty($random)){
			if(empty($gid)){
				$this->jump('/index.php/main/index');
			} 
			// 以下补救以前的问题
			$tgm = $gModel->find(array('id'=>$gid));  
			if(empty($tgm)){
				$this->jump('/index.php/main/index');
			}
			//如果不存在唯一标识 则生成标识
			if(empty($tgm['uniokey'])){
				$trandom = mt_rand(10000000,99999999);
				$gModel->update(array('id'=>$gid),array('uniokey'=>$trandom));
				$url = '/index.php/main/talk/random/'.$trandom; 
				$this->jump($url);
			}//如果存在 则中转到新的地址规则
			$url = '/index.php/main/talk/random/'.$tgm['uniokey']; 
			$this->jump($url); 
		}
		$gm = $gModel->find(array('uniokey'=>$random));
		if(empty($gm)){
			$this->jump('/index.php/main/index');
		}
		$this->gm = $gm;
		$this->gid = $gm['id'];
		$this->openid = $uid;
		$this->xielie = $gm['xielie'];
		$uModel = spClass('m_wx_user');
		$user = $uModel->find(array('openid'=>$gm['uid']));
		$user['nickname'] = base64_decode($user['nickname']);
		$this->user = $user;
		$my = $uModel->find(array('openid'=>$uid));
		$my['nickname'] = base64_decode($my['nickname']);
		$this->my = $my;
		if($gm['uid']==$my['openid']){
			$this->jump('/index.php/main/view');
		}
	}
	
	function result(){
		//$this->chkMobile();
		$gid = $this->spArgs('gid');
		$money = $this->spArgs('money');
		if(empty($gid)||empty($gid)){
			exit('Error');
		}
		$gModel = spClass('m_wx_game');
		$gm = $gModel->find(array('id'=>$gid));
		$this->gm = $gm;
		$uModel = spClass('m_wx_user');
		$user = $uModel->find(array('openid'=>$gm['uid']));
		$this->money = $money;
		$user['nickname'] = base64_decode($user['nickname']);
		$this->user = $user;
		$lModel = spClass('m_wx_log'); 
		$lst = $lModel->findAll(array('game_id'=>$gid),'money desc','openid,money','0,3');
		$list = array();
		foreach($lst as $lt){
			$tu = $uModel->find(array('openid'=>$lt['openid']));
			$tar = $lt;
			$tar['user_name'] = base64_decode($tu['nickname']);
			$tar['user_img'] = $tu['headimgurl'];
			$list[] = $tar;
		}
		$this->datalist = $list;
	}
	
	function view(){
		//$this->chkMobile();
		$tid = $this->spArgs('openid');
		$isMy = false;
		if(!empty($tid)){
			$uid = $tid;
			if($uid==$_COOKIE['UID']){
				$this->jump('/index.php/main/view');
			}
		}else{
			$uid = $_COOKIE['UID']; 
			$isMy = true;
		}
		$this->isMy = $isMy;
		if(empty($uid))
			exit('Empty');
		$gModel = spClass('m_wx_game');
		$gm = $gModel->find(array('uid'=>$uid));
		if(empty($gm)){
			exit('error');
		}
		$this->gm = $gm;
		$uModel = spClass('m_wx_user');
		$user = $uModel->find(array('openid'=>$uid));
		$user['nickname'] = mb_substr(base64_decode($user['nickname']),0,21,'UTF-8');
		$this->user = $user;
		$pm = $gModel->findCount('in_phd = 2 and money>'.$gm['money']);
		$pm = $pm>0?$pm:'0';
		$this->pm = $pm;
		
		$lModel = spClass('m_wx_log');
		$list = $lModel->findAll(array('game_id'=>$gm['id']),null,'openid,money,created');
		$arr = array();
		foreach($list as $li){
			$tlis = $li;
			$us = $uModel->find(array('openid'=>$li['openid']),null,'openid,nickname,headimgurl');
			$tlis['user_openid'] = $us['openid'];
			$tlis['user_name'] = mb_substr(base64_decode($us['nickname']),0,21,'UTF-8');
			$tlis['user_img'] = $us['headimgurl'];
			$arr[] = $tlis;
		}
		$this->datalist = $arr;
		
		$url = 'http://pingan.weixinzjit.com/index.php/main/view';
		$signPackage = $this->Wechat->getJsSign($url);		
		$this->JK = $signPackage;
	}
	 
	
	function loading(){
	}
	
	/**
	* 获取人品榜数据
	* @return
	*/
	function getRpb(){
		$gModel = spClass('m_wx_game');
		$uModel = spClass('m_wx_user'); 
		$list = $gModel->findAll(array('in_phd'=>2),'money desc','uid,money','0,20');
		$data = array();
		 
		
		foreach($list as $li){
			$tli = $li;
			$us = $uModel->find(array('openid'=>$li['uid']),null,'openid,nickname,headimgurl');
			$tli['user_openid'] = $us['openid'];
			$tli['user_name'] = mb_substr(base64_decode($us['nickname']),0,21,'UTF-8');
			$tli['user_img'] = $us['headimgurl']; 
			$data[] = $tli;
		}
		FN::_Ajax_return(true,'success',$data);
	}
	
	/**
	* 发起借钱活动
	* @return
	*/
	function saveGame(){ 
	
		$word = $this->spArgs("word"); 
		$openid = $this->spArgs("openid");
		$xielie = $this->spArgs('xielie');
		$random = $this->spArgs('random');
		
		if(empty($word)||empty($openid)){
			FN::_Ajax_return(false,'empty key');
		}
		
		$gModel = spClass('m_wx_game'); 
		$data = array(); 
		$data['uid'] = $openid; 
		$ct = $gModel->find($data); 
		$data['word'] = $word;
		$data['money'] = 0.00;
		$data['xielie'] = $xielie;
		$data['xielie'] = $xielie;
		$data['uniokey'] = $random;
		  
		//如果已存在 则修改 否则插入
		if(!empty($ct)){
			$rs = $gModel->update($data,array('id'=>$ct['id']));
			if($rs){
				FN::_Ajax_return(true,'操作成功',$ct['id']);
			}else{
				FN::_Ajax_return(true,'操作失败');
			}
		}else{
			$rs = $gModel->create($data);
			if($rs){
				FN::_Ajax_return(true,'操作成功',$rs);
			}else{
				FN::_Ajax_return(true,'操作失败');
			}
		}
	}
	
	/**
	* 借给好友钱
	* @return
	*/
	function saveMoney(){
		$openid = $this->spArgs("openid");
		$money = $this->spArgs("money");
		$gid = $this->spArgs("gid"); 
		 
		//插入日志
		$lModel = spClass('m_wx_log');
		$gModel = spClass('m_wx_game');
		$gm = $gModel->find(array('id'=>$gid));
		if(empty($gm)){
			exit('Error');
		}
		
		$lg = $lModel->find(array('openid'=>$openid,'game_id'=>$gid));
		if(!empty($lg)){
			FN::_Ajax_return(false,'你已给TA转过账');
		}
		
		$data = array(
			'game_id'	=>	$gid,
			'openid'	=>	$openid,
			'uid'		=>	$gm['uid'],
			'money'		=>	$money
		);
		$lModel->create($data);
		//更新记录
		$gModel->incrField(array('id'=>$gid),'money',$money);
		FN::_Ajax_return(true,'succes');
	}
	
	function getList(){
		$gid = $this->spArgs('gid');
		$lModel = spClass('m_wx_log');
		$uModel = spClass('m_wx_user');
		$list = $lModel->findAll(array('game_id'=>$gid),null,'openid,money,created');
		$arr = array();
		foreach($list as $li){
			$tlis = $li;
			$us = $uModel->find(array('openid'=>$li['openid']),null,'openid,nickname,headimgurl');
			$tlis['user_openid'] = $us['openid'];
			$tlis['user_name'] =  mb_substr(base64_decode($us['nickname']),0,21,'UTF-8');
			$tlis['user_img'] = $us['headimgurl'];
			$arr[] = $tlis;
		}
		FN::_Ajax_return(true,'success',$arr);
	}
	
	/**
	* 微信授权登陆
	* @return
	*/
	function wlogin(){
		$ul = $_GET['turl']; 
		$callback = 'http://pingan.weixinzjit.com/index.php?c=main&a=wloginback&turl='.utf8_encode($ul);
		$url = $this->Wechat->getOauthRedirect($callback); 
		echo '<script>location.href="'.$url.'";</script>'; 
		exit();
	}
	
	/**
	* 授权登陆成功回调地址
	* @return
	*/
	function wloginback(){ 
		//如果设置返回地址 则转到返回地址  否则跳转到game页面 
		$turl = empty($_GET['turl'])?'':$_GET['turl'];   
		//获取授权TokenCode
		$tokeninfo = $this->Wechat->getOauthAccessToken();
		if(empty($tokeninfo)){
			exit('get code error');
		}
		//获取用户详细信息
		$userinfo = $this->Wechat->getOauthUserinfo($tokeninfo['access_token'],$tokeninfo['openid']);
		if(empty($userinfo)){
			exit('get userinfo error');
		}
		//记录登陆用户OPENID
		setcookie("UID", $userinfo['openid'], time()+24*3600);
		$userData = array(
			'openid' => $userinfo['openid'],
			'nickname' => base64_encode($userinfo['nickname']),
			'sex' => $userinfo['sex'],
			'province' => $userinfo['province'],
			'city' => $userinfo['city'],
			'country' => $userinfo['country'],
			'headimgurl' => $userinfo['headimgurl'],
			'privilege' => json_encode($userinfo['privilege']),
			'unionid' => $userinfo['unionid'],
			'addtime'	=>	date('Y-m-d H:i:s',time()),
		);
		
		//用户数据入库  如果用户数据不存在则插入数据到用户库
		$userModel = spClass('m_wx_user');
		$uinfo = $userModel->findBy('openid',$userinfo['openid']);
		if(empty($uinfo)){
			$rs = $userModel->create($userData);
		} 
		if(!empty($turl)){
			$this->jump($turl); // 跳转到微信登陆页面
		}else{
			$this->jump('/index.php/main/index'); // 跳转到微信登陆页面
		}
	}
	
	function notwx(){
		$this->mes = '请在微信浏览器中访问此页面！';
	}
	
	function __construct(){
		parent::__construct();
		/*
		if('notwx'!=$_GET['a']){
			$user_agent = $_SERVER['HTTP_USER_AGENT']; 
			if(strpos($user_agent, 'MicroMessenger') === false){
				// 非微信浏览器禁止浏览 
				$this->jump('/index.php?c=main&a=notwx');
			} else{
				// 微信浏览器，允许访问
			}
		} 
		*/
		//微信信息配置  我的测试号码
		$options = array(
			'appid'=>'wx4b2925e58d962b51',
			'token'=>'nicky123', 
			'encodingaeskey'=>'',
			'appsecret'=>'7a23acbaf9ab5d6582cbfa1cdff0954e',
			'debug'=>false,
			'logcallback'=>'',
		);
		$this->Wechat = spClass('Wechat'); 
		$this->Wechat = new Wechat($options); 
	} 
	
	/**
	* 获取微信分享的配置信息
	* @return
	*/
	function getJsConf(){
		$url = $this->spArgs("url");
		if(empty($url)){
			FN::_Ajax_return(false,'');
		}
		$signPackage = $this->Wechat->getJsSign($url);
		FN::_Ajax_return(true,'',$signPackage);
	}  
	
	function chkMobile(){
		$user_agent = $_SERVER['HTTP_USER_AGENT']; 
		if(strpos($user_agent, 'MicroMessenger') === false){
			// 非微信浏览器禁止浏览 
			$this->jump('/index.php?c=main&a=notwx');
		} else{
			// 微信浏览器，允许访问
		}
	}
	
	function lgt(){
		setcookie('UID',null,time()-100);
		setcookie("URL", null, time()-100); 
		$this->jump('/index.php/main/index');
	}
	 
}